package com.example.shop.home

import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.bumptech.glide.Glide
import com.example.base.entity.CategoryEntity
import com.example.base.entity.GoodsEntity
import com.example.base.mvvm.BaseFragment
import com.example.shop.R
import com.example.shop.adapter.CategoryAdapter
import com.example.shop.adapter.GoodsAdapter
import com.example.shop.databinding.FragmentHomeBinding
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.youth.banner.BannerConfig
import com.youth.banner.loader.ImageLoader


class HomeFragment : BaseFragment<FragmentHomeBinding,HomeViewModel>() {
    val imglist = mutableListOf<String>()
    val catelist = mutableListOf<CategoryEntity>()
    lateinit var categoryAdapter: CategoryAdapter
    //请求商品用到的常量
    var categoryId = 14
    var page = 1
    val goodslist = mutableListOf<GoodsEntity>()
    lateinit var goodsAdapter: GoodsAdapter
    override fun initView() {
        viewModel.banner()

        categoryAdapter = CategoryAdapter(catelist)
        binding.cateRv.adapter = categoryAdapter
        binding.cateRv.layoutManager = GridLayoutManager(context,5)
        viewModel.category()//请求分类

        goodsAdapter = GoodsAdapter(goodslist)
        binding.goodRv.adapter = goodsAdapter
        binding.goodRv.layoutManager = GridLayoutManager(context,2)
        viewModel.goods(categoryId,page,10)
        //刷新加载更多功能
        binding.smart.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                viewModel.goods(categoryId,page,10)
                refreshLayout.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                viewModel.goods(categoryId,page,10)
                refreshLayout.finishLoadMore()
            }
        })
        //点击上方分类，下方商品数据发生变化
        categoryAdapter.setOnItemClickListener { adapter, view, position ->
            categoryId = catelist[position].id
            page=1
            viewModel.goods(categoryId,page,10)
        }
        //点击商品item,跳转到详情界面
        goodsAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/DetailActivity").withInt("goodsId",goodslist[position].id).navigation()
        }
        binding.clSearch.setOnClickListener {
            ARouter.getInstance().build("/app/SearchActivity").navigation()
        }
    }

    override fun initData() {
        viewModel.bannerSuccess.observe(this){
            if(it.code==200){
                imglist.clear()
                it.data.forEach {
                    //获取请求数据中的图片地址
                    imglist.add(it.imagePath)
                }
                binding.banner.setImages(imglist)
                binding.banner.setImageLoader(object :ImageLoader(){
                    override fun displayImage(
                        context: Context?,
                        path: Any?,
                        imageView: ImageView?
                    ) {
                        if(context!=null&&path!=null){
                            imageView?.let {
                                Glide.with(context).load(path.toString()).into(it)
                            }
                        }

                    }
                })
                //轮播图轮播时间
                binding.banner.setDelayTime(2000)
                //轮播图风格
                binding.banner.setBannerStyle(BannerConfig.NUM_INDICATOR)
                binding.banner.start()
            }
        }
        viewModel.categorySuccess.observe(this){
            if(it.code==200){
                catelist.clear()
                val data = it.data //获取的是电脑办公、手机数码、男装、女装
                val cateData = data[0]//获取的是电脑办公下方的分类
                if(cateData!=null){
                    catelist.addAll(cateData.data)
                    categoryAdapter.notifyDataSetChanged()
                }
            }
        }
        viewModel.goodsSuccess.observe(this){
            if(it.code==200){
                if(page==1){
                    goodslist.clear()
                }
                goodslist.addAll(it.data)
                goodsAdapter.notifyDataSetChanged()
            }
        }


    }

    override fun getViewModel(): Class<HomeViewModel>  = HomeViewModel::class.java

    override fun getLayout(): Int = R.layout.fragment_home


}